package org.example.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.exception.AppException;
import org.example.exception.AppExceptionCodeMsg;
import org.example.pojo.Permission;
import org.example.query.PermissionQuery;
import org.example.service.impl.PermissionServiceImpl;
import org.example.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author peak
 * @since 2024-12-19
 */
@Api(description = "权限管理")
@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController<Permission,PermissionQuery> {
    @Autowired
    private PermissionServiceImpl permissionService;
    @ApiOperation("通过角色Id查询对应权限")
    @PostMapping("/role/{roleId}")
    public ResultBean selectByRoleId(@PathVariable("roleId") Long roleId ) {
        if(roleId==null || roleId==0) {
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        PermissionQuery permissionQuery = new PermissionQuery();
        permissionQuery.setRoleId(roleId);
        List list = permissionService.list(permissionQuery.getQueryWrapper());
        return  ResultBean.SUCCESS(list);
    }
    @ApiOperation(value = "批量删除" )
    @GetMapping("/batch1/{ids}")
    public ResultBean getBatchDel(@PathVariable("ids") List<Integer> list) throws Exception {
        //因为在前端的时候，直接变成19，21的形式了，在后端只需要进行转换即可
        boolean flg=permissionService.removeByIds(list);
        if(!flg){
            throw new AppException(AppExceptionCodeMsg.SAVE_FAIL);
        }
        return ResultBean.SUCCESS();
    }
}
